<template>
	<view class="login-content">
		<view class="title">
			登錄
		</view>
		<view class="tips">
			還沒有賬號，<text @click="toRegister"  style="color:#0055ff">立即註冊</text>
		</view>
		<view class="infor-content">
			<view class="item">
				<input type="tel" v-model="tel" placeholder-style="color:#9E9E9E" placeholder="请输入手机号">
			</view>
			<view class="item" v-if="isCodeLogin">
				<input type="text" v-model="code" placeholder="请输入驗證碼" placeholder-style="color:#9E9E9E" >
				<view class="send-code" v-if="!haveSend" @click="sendCode">發送驗證碼</view>
				<view class="countDown" @click="sendCode" v-if="haveSend">
					重新發送<text v-if="countNum>0">（{{countNum}}s）</text>
				</view>
			</view>
			<view class="item" v-else>
				<input type="password" v-model="password" placeholder="请输入密码" placeholder-style="color:#9E9E9E" >
			</view>
		</view>
		<view class="login-btn" @click="loginOk">
			登錄
		</view>
		<view class="code" @click="isCodeLogin=true" v-if="!isCodeLogin" >短信驗證</view>
		<view class="code" @click="isCodeLogin=false" v-else>密碼驗證</view>
		<view class="agreement" @click="toAgress" >
			<view class="choose" v-show="!isAgree"></view>
			<view class="checked" v-show="isAgree">
				<image src="../../static/image/login/check.png" mode=""></image>
			</view>
			<view class="text">註冊即同意<text @click.stop="showAgreement('driver_apply_content')">《司機註冊協議》</text><text @click.stop="showAgreement('privacy_policy')">《隱私政策》</text></view>
		</view>
	</view>
</template>

<script>
	import { login,codeLogin } from '../../request/api.js'
	export default {
		data() {
			return {
				tel:'',
				isAgree:false,
				tel:"",
				password:'',
				code:"",
				haveSend:false,
				countNum:0,
				isCodeLogin:false,
			};
		},
		onPullDownRefresh() {
			uni.stopPullDownRefresh();
		},
		methods:{
			// 顯示政策
			showAgreement(e){
				uni.navigateTo({
					url:'/pages/agreement/agreement?type='+e
				})
			},
			toAgress(){
				this.isAgree=!this.isAgree
			},
			toRegister(){
				uni.navigateTo({
					url:'/pages/register/register'
				})
			},
			// 登录
			async loginOk(){
				if(!this.isAgree){
					uni.showToast({
						title: '請閱讀並同意用戶協議',
						icon: 'none'
					})
					return
				}
				if(!this.tel){
					uni.showToast({
						title: '請填寫手機號',
						icon: 'none'
					})
					return
				}
				if (!/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/.test(this.tel)){
					uni.showToast({
						title: '請填寫正確手機號',
						icon: 'none'
					})
					return
				}
				if(!this.password&&!this.isCodeLogin){
					uni.showToast({
						title: '請填寫密碼',
						icon: 'none'
					})
					return
				}
				if(!this.code&&this.isCodeLogin){
					uni.showToast({
						title: '請填寫驗證碼',
						icon: 'none'
					})
					return
				}
				uni.showLoading({
					title:'登錄中...'
				})
				if(this.isCodeLogin){
					let res=await codeLogin({
						mobile:this.tel,
						captcha:this.code
					})
					uni.hideLoading();
					console.log(res);
					if(res.code==1){
						uni.showToast({
							title: '登錄成功',
							icon: 'none'
						})
						uni.setStorageSync('token', res.data.userinfo.token);
						this.$store.commit("user/CHANGE_MY_INFOR",res.data.userinfo);
						this.$store.commit('user/CHANGE_LOGIN_STATUS',true);
						uni.redirectTo({
							url:'/pages/index/index'
						})
					}else{
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}else{
					let res=await login({
						account:this.tel,
						password:this.password
					})
					uni.hideLoading();
					console.log(res);
					if(res.code==1){
						uni.showToast({
							title: '登錄成功',
							icon: 'none'
						})
						uni.setStorageSync('token', res.data.userinfo.token);
						uni.setStorageSync('myInfor',JSON.stringify(res.data.userinfo));
						this.$store.commit("user/CHANGE_MY_INFOR",res.data.userinfo)
						this.$store.commit('user/CHANGE_LOGIN_STATUS',true);
						uni.redirectTo({
							url:'/pages/index/index'
						})
					}else{
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}
			},
			// 发送验证码
			sendCode(){
				if(this.countNum>0){
					return;
				}
				this.haveSend=true;
				this.countNum=60;
				let timeInter=setInterval(()=>{
					if(this.countNum>0){
						this.countNum--;
					}else{
						clearInterval(timeInter);
					}
				},1000)
			}
		}
	}
</script>

<style lang="scss">
	page{
		background-color: #FAFAFA;
		.login-content{
			width:650rpx;margin-left:auto;
			margin-right:auto;
			padding-top:190rpx;
			.code {
				font-size: 28rpx;
				color: #9E9E9E;
				margin-top: 30rpx;
				line-height: 40rpx;
				text-align: center;
			}
			.title{
				font-size: 40rpx;line-height:56rpx;
				color:#040404;
			}
			.tips{
				font-size:24rpx;line-height:33rpx;
				color:#9E9E9E;margin-top:15rpx;
			}
			.infor-content{
				margin-top:130rpx;
				.item{
					padding:40rpx 0;border-bottom:1px solid #F0F0F0;
					display: flex;align-items: center;
					input{
						font-size:28rpx;color:#333333;flex:1;
					}
					.send-code{
						font-size: 28rpx;color:#333333;
					}
					.countDown{
						font-size: 28rpx;color:#D71B0A;
					}
				}
			}
			.login-btn{
				width:100%;height:98rpx;border-radius: 98rpx;
				display: flex;align-items: center;justify-content: center;
				font-size:36rpx;color:#ffffff;background-color:#040404;
				margin-top:120rpx;
			}
			.agreement{
				display: flex;align-items:center;position: absolute;left:0;
				bottom:50rpx;width:100%;justify-content: center;
				font-size:24rpx;color:#9E9E9E;
				.choose{
					width:26rpx;height:26rpx;border-radius: 50%;
					border:1px solid #9E9E9E;box-sizing: border-box;
					margin-right:10rpx;
				}
				text{
					color:#040404
				}
				.checked{
					width:26rpx;height:26rpx;margin-right:10rpx;
					image{
						width:26rpx;height:26rpx;
					}
				}
			}
		}
	}
</style>
